import { fileURLToPath, URL } from "url";
import path, { resolve } from "path";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import {
  createStyleImportPlugin,
  ElementPlusResolve,
} from "vite-plugin-style-import";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import vueSetupExtend from "vite-plugin-vue-setup-extend";

// https://vitejs.dev/config/
export default defineConfig({
  // base: '/admin/',
  server: {
    host: "0.0.0.0",
    // Listening on all local IPs
    port: 3000,
    // Load proxy configuration from .env
    proxy: {
      "/system": {
        target: "http://127.0.0.1:8081",
        changeOrigin: true,
        ws: false,
        // rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
        // https is require secure=false
      },
      "/xxl-job-admin": {
        target: "http://127.0.0.1:8881",
        changeOrigin: true,
        ws: false,
        // rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
        // https is require secure=false
      },
    },
  },
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      imports: ["vue", "vue-router"],
      resolvers: [ElementPlusResolver()],
      eslintrc: {
        enabled: true,
      },
    }),
    Components({
      directoryAsNamespace: true,
      resolvers: [ElementPlusResolver()],
    }),
    createStyleImportPlugin({
      resolves: [ElementPlusResolve()],
    }),
    createSvgIconsPlugin({
      // 配置路劲在你的src里的svg存放文件
      iconDirs: [fileURLToPath(new URL("./src/assets/icons", import.meta.url))],
      symbolId: "local-icon-[dir]-[name]",
    }),
    vueSetupExtend(),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
      "/@": fileURLToPath(new URL("./src", import.meta.url)),
      "~": resolve(__dirname, "./"),
      //   "@": resolve(__dirname, "src"),
      src: resolve(__dirname, "src"),
    },
    extensions: [".js", ".ts", ".jsx", ".tsx", ".json", ".vue", ".mjs"],
  },
  optimizeDeps: {
    include: [
      //   "element-plus/lib/locale/lang/zh-cn",
      "element-plus/es/locale/lang/zh-cn",
      "bpmn-js/lib/Modeler",
      "bpmn-js-token-simulation",
    ],
  },
  build: {
    assetsDir: "static",
    minify: "terser",
    // 消除打包大小超过500kb警告
    chunkSizeWarningLimit: 2000,
    commonjsOptions: {
      include: "/node_modules|lib/",
    },
    terserOptions: {
      compress: {
        drop_console: true, //移除console
        drop_debugger: true, //移除debug
      },
    },
    rollupOptions: {
      manualChunks(id) {
        if (id.includes("node_modules")) {
          return id
            .toString()
            .split("node_modules/")[1]
            .split("/")[0]
            .toString();
        }
      },
    },
  },
});
